chrome.storage.local.get(['Enabled', 'Auto_expand', 'Enable_copy', 'Clear_doms'], function(value) {
    if (!value.Enabled) {
        console.log("未启用扩展功能");
        return;
    }
    console.log("扩展开始执行");

    if (value.Enable_copy) {
        console.log("执行破解复制");
        enableCopy2();
    }

    console.log("执行破解关注");
    if (value.Auto_expand) {
        expand(); //自动展开
    } else {
        modify(); //修改防止默认跳转
    }

    if (value.Clear_doms) {
        console.log("执行清除弹窗");
        removeDoms(); //移除登录弹窗等

        /**
         * 动态监听dom变化，移除弹窗
         */
        //创建新的dom监听器
        let observer = new MutationObserver((mutationsList) => {
            for (const mutation of mutationsList) {
                if (mutation.type == 'childList') { //只监听一级
                    removeDoms();
                }
            }
        });
        // 开始监听属性变化
        observer.observe(document.body, {
            childList: true
        });
    }
});

function modify() {
    let follow_text = document.getElementsByClassName('follow-text');
    if (follow_text.length == 0) {
        return;
    }
    follow_text = follow_text[0];
    if (follow_text.textContent == "关注博主即可阅读全文") {
        follow_text.addEventListener("click", (event) => {
            event.stopPropagation(); //禁止默认跳转
            expand(); //展开
        });
        let img = document.getElementsByClassName('chevrondown')[0]; //"关注博主即可阅读全文"旁向下的箭头图标
        img.addEventListener("click", (event) => {
            event.stopPropagation(); //禁止默认跳转
            expand(); //展开
        });
        follow_text.textContent = "关注限制已解锁";
        console.log("关注限制已解锁");
    } else {
        console.log("没有找到\"关注博主即可阅读全文\"");
    }
}

function expand() {
    let article_content = document.getElementById("article_content");
    let hide_article_box = document.getElementsByClassName('hide-article-box')[0];
    article_content.removeAttribute("style");
    hide_article_box.parentElement.removeChild(hide_article_box);
    console.log("全文展开成功");
}

function removeDoms() {
    /**
     * 根据class移除一些dom
     */
    let classes = ['passport-login-tip-container', 'passport-login-container', 'tool-active-list', 'article-search-tip',
        'hljs-button', 'csdn-side-toolbar', 'box-shadow mb8', 'blog-footer-bottom'];
    let infos = ['右下角弹窗', '登录弹窗', '一键收藏', '黑色提示框', 
        '登录复制提示', '侧边工具栏', '左侧广告', '底部备案信息'];
    classes.forEach((cls, i) => {
        let box = document.getElementsByClassName(cls)[0];
        if (box) {
            box.parentElement.removeChild(box);
            console.log(">>>>移除" + infos[i]);
        }
    });
    /**
     * 根据id移除一些dom
     */
    let ids = ['toolbarBox', 'asideWriteGuide', 'asideNewNps', 'recommendNps'];
    let names = ["顶部栏", '左侧广告', '左侧是否推荐','底部是否推荐'];
    ids.forEach((id, i) => {
        let box = document.getElementById(id);
        if (box) {
            box.parentElement.removeChild(box);
            console.log(">>>>移除" + names[i]);
        }
    });
}

/**
 * 启用复制功能方法一
 * 参考：https://www.cnblogs.com/pu369/p/17425742.html
*/
function enableCopy() {
    function setAllSelect(el = document.body) {
        for (let index = 0; index < el.children.length; index++) {
            const e = el.children.item(index);
            e.style.userSelect = 'text';
            setAllSelect(e);
        }
    }
    setAllSelect();

    //监听键盘Ctrl+C
    document.body.onkeydown = function(e) {
        if (e.ctrlKey && e.keyCode == 67) {
            const pasteText = window.getSelection().toString();
            if (null === pasteText || undefined === pasteText || '' === pasteText.trim()) {
                return;
            }
            navigator.clipboard.writeText(pasteText).then(() => {
                console.log("复制成功！"); //alert("复制失败！");
            }).catch(() => {
                console.log("复制失败！");
            });
        }
    }
    //监听右键复制无法实现
}

/**
 * 启用复制功能方法二
 * 参考：https://blog.csdn.net/weixin_49119066/article/details/139220307
*/
function enableCopy2() {
    window.oncontextmenu = document.oncontextmenu = document.oncopy = null;
    var hea = document.getElementsByClassName('toolbar-advert')[0];
    if (hea) {
        hea.remove()
    };
    [...document.querySelectorAll('body')].forEach(dom => dom.outerHTML = dom.outerHTML);
    [...document.querySelectorAll('body, body *')].forEach(dom => {
        ['onselect', 'onselectstart', 'onselectend', 'ondragstart', 'ondragend', 'oncontextmenu', 'oncopy'].forEach(ev => dom.removeAttribute(ev));
        dom.style['user-select'] = 'auto';
    });
}